// const winston = require('../../../../tasks/util/winstonLogger.js');
orderModalController.$inject = ['$scope', '$location', 'orderService', 'order', '$uibModalInstance','$interval','confirmOrder'];
function orderModalController($scope, $location, orderService, order, $modalInstance, $interval,confirmOrder) {
    // var vm = this;
    // vm.uname = '';
    // vm.password = '';
    // console.log(merchant);
    $scope.order = order;
    var vm = this;

    if($scope.order.status=="正在配送"){
        let pre = new Date($scope.order.createDate).valueOf();
        var timer = $interval(function () {
            let cur =  new Date().valueOf();
            // console.log(60000-cur+pre);
            if((180000-cur+pre)<=0){
                confirmOrder($scope.order);
                // $interval.cancel(timer);
                return;
            }else{
                $scope.now = new Date(180000-cur+pre);
            }
            
        }, 1000);
    }
    
    
    $scope.cancel = function () {
        console.log("close the modal");
        $modalInstance.dismiss('cancel');
    }

    $scope.cancelOrder = function (status) {
        console.log("enter the pass method");
        
        var r = confirm("您确定取消该订单？");
        if (r == true) {
            //start to update
            order.status = "订单取消";
            orderService.updateOrderStatus(order, function (err, result) {
                if (err) {
                    console.log("error " + err.message);
                    order.status=status;
                    alert("sorry,出错了");
                }
                else {
                    console.log(result);
                    if (result.nModified == 1 && result.ok == 1) {
                        console.log("success to call back");
                        $scope.cancel();
                    }
                    else {
                        console.log("fail to update");
                        alert("sorry,出错了");
                        order.status=status;
                    }

                }
            })
        }
        else {
            return false;
        }

    };


}
module.exports = orderModalController;

